package com.lesso.yjy.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.yjy.sunflower.domain.repository.EmployeeAttendanceRuleRepository;
import com.lesso.yjy.sunflower.infra.assembler.EmployeeAssembler;
import com.lesso.yjy.sunflower.infra.assembler.EmployeeAttendanceRuleAssembler;
import com.lesso.yjy.sunflower.infra.mapper.EmployeeAttendanceRuleMapper;
import com.lesso.yjy.sunflower.model.Employee;
import com.lesso.yjy.sunflower.model.EmployeeAttendanceRule;
import com.lesso.yjy.sunflower.pojo.dto.EmployeeAttendanceRuleDTO;
import com.lesso.yjy.sunflower.pojo.dto.EmployeeAttendanceRuleQueryDTO;
import com.lesso.yjy.sunflower.pojo.dto.EmployeeQueryDTO;
import com.lesso.yjy.sunflower.pojo.vo.EmployeeAttendanceRuleVO;
import com.lesso.yjy.sunflower.pojo.vo.EmployeeVO;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Repository
public class EmployeeAttendanceRuleRepositoryImpl implements EmployeeAttendanceRuleRepository {
    private final EmployeeAttendanceRuleMapper employeeAttendanceRuleMapper;

    public EmployeeAttendanceRuleRepositoryImpl(EmployeeAttendanceRuleMapper employeeAttendanceRuleMapper){
        this.employeeAttendanceRuleMapper = employeeAttendanceRuleMapper;
    }

    @Override
    @Transactional
    public Long save(EmployeeAttendanceRule employeeAttendanceRule) {
        if (employeeAttendanceRuleMapper.checkEmployeeExists(employeeAttendanceRule.getEmployeeId())<=0){
            throw new RuntimeException("员工不存在");
        }
        if (employeeAttendanceRuleMapper.checkRuleExists(employeeAttendanceRule.getRuleCode())<=0){
            throw new RuntimeException("考勤规则不存在");
        }
        employeeAttendanceRule.setDelStatus(0);
        employeeAttendanceRuleMapper.insert(employeeAttendanceRule);
        return employeeAttendanceRule.getId();
    }

    @Override
    public Page<EmployeeAttendanceRuleVO> getPageList(EmployeeAttendanceRuleQueryDTO dto) {
        Page<EmployeeAttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<EmployeeAttendanceRule> wrapper = new EntityWrapper<>();
        wrapper.eq(EmployeeAttendanceRule.EMPLOYEE_ID, dto.getEmployeeId());
        List<EmployeeAttendanceRule> employeeAttendanceRule = employeeAttendanceRuleMapper.selectPage(page, wrapper);
        return page.setRecords(EmployeeAttendanceRuleAssembler.INSTANCE.entityListToVOList(employeeAttendanceRule));
    }

    @Override
    public Page<EmployeeAttendanceRuleVO> getCustomPageList(EmployeeAttendanceRuleQueryDTO dto) {
        Page<EmployeeAttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        List<EmployeeAttendanceRuleVO> list = employeeAttendanceRuleMapper.selectPageList(page, dto);
        return page.setRecords(list);
    }

    @Override
    public void removeById(Long id) {
        employeeAttendanceRuleMapper.deleteById(id);
    }

    @Override
    public void updateById(EmployeeAttendanceRuleDTO dto) {
        EmployeeAttendanceRule employeeAttendanceRule = EmployeeAttendanceRuleAssembler.INSTANCE.dtoToEntity(dto);
        employeeAttendanceRuleMapper.updateById(employeeAttendanceRule);
    }
}
